<?php

namespace App\Models\Cruise;

use App\Models\BaseModel;
use App\Models\Common\ResourcesModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use App\Support\CustomForm\AttributeCruiseSupport;
use App\Support\CustomForm\AttributeSupport;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Cache;

class CruiseTemplateModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;

    protected $table = 'cruise_template';

    protected $fillable = [
        'id',
        'name',
        'desc',
        'status',
        'type',
        'sysTemplate',
        'antiCheatingSwitch'
    ];

    const STATUS_OFF  = 0;
    const STATUS_ON   = 1;
    const STATUS_TEXT = [
        self::STATUS_ON  => "禁用",
        self::STATUS_OFF => "启用",
    ];

    const TYPE_1 = 1;
    const TYPE_2 = 2;
    const TYPE_3 = 3;
    const TYPE_4 = 4;
    const TYPE_5 = 5;
    const TYPE_6 = 6;
    const TYPE_7 = 7;
    const TYPE_8 = 8;
    const TYPE_9 = 9;
    const TYPE_99 = 99;  //其他

    const TYPE_TEXT = [
        self::TYPE_1 => '增长',
        self::TYPE_2 => '选址',
        self::TYPE_3 => '门店稽核',
        self::TYPE_4 => '商学院',
        self::TYPE_5 => '工厂稽核',
        self::TYPE_6 => '云巡店',
        self::TYPE_7 => '审批流自定义',
        self::TYPE_8 => '门店自检',
        self::TYPE_9 => '品牌运营项目',
        self::TYPE_99 => '其他',
    ];

    const TYPE_RESOURCE_PARAM = [
        self::TYPE_1 => [ResourcesModel::MODULE_TYPE_CRUISE, ResourcesModel::BUSINESS_CRUISE_ORDERS],
        self::TYPE_2 => [ResourcesModel::MODULE_TYPE_SITE, ResourcesModel::BUSINESS_SITE_ORDERS],
        self::TYPE_3 => [ResourcesModel::MODULE_TYPE_CHECK, ResourcesModel::BUSINESS_CHECK_ORDERS],
        self::TYPE_4 => ['', ''],
        self::TYPE_5 => ['', ''],
        self::TYPE_6 => [ResourcesModel::MODULE_TYPE_TOUR, ResourcesModel::BUSINESS_OPEN_TOUR_INPUT],
        self::TYPE_7 => ['', ''],
        self::TYPE_8 => ['', ''],
        self::TYPE_9 => ['', ''],
        self::TYPE_99 => [ResourcesModel::MODULE_TYPE_SITE_MALL, ResourcesModel::BUSINESS_SITE_LOCATION_MALL],
    ];

    const SYS_TEMPLATE_1       = 1;
    const SYS_TEMPLATE_SITE_1  = 10;
    const SYS_TEMPLATE_SITE_2  = 11;
    const SYS_TEMPLATE_SITE_3  = 12;
    const SYS_TEMPLATE_SITE_4  = 13;
    const SYS_TEMPLATE_SITE_5  = 14;
    const SYS_TEMPLATE_SITE_6  = 15;
    const SYS_TEMPLATE_SITE_7  = 16;
    const SYS_TEMPLATE_COMMON  = 20;
    const SYS_TEMPLATE_SPECIAL = 21;
    const SYS_TEMPLATE_COLLEGE = 30;
    const SYS_TEMPLATE_FACTORY_SELF = 40;
    const SYS_TEMPLATE_FACTORY_SPECIAL = 41;

    const SYS_TEMPLATE_WORKFLOW_RENEW = 60;
    const SYS_TEMPLATE_WORKFLOW_WORK_ORDER = 61;

    const SYS_TEMPLATE_TOUR = 50; //云巡店

    // 门店自检相关
    const SYS_TEMPLATE_SELF_OPEN = 80;
    const SYS_TEMPLATE_SELF_CLOSE = 81;
    const SYS_TEMPLATE_SELF_WEEK = 82;
    const SYS_TEMPLATE_SELF_MONTH = 83;

    const SYS_TEMPLATE_ACTIVITY_ACTION = 90; // 项目行为

    const SYS_TEMPLATE_MALL_INFO = 200; //商场店
    const SYS_TEMPLATE_OTHER_MYBANK_REGISTER = 201; // 网商进件

    const SYS_TEMPLATE_TEXT = [
        self::SYS_TEMPLATE_1 => '新店帮扶',

        self::SYS_TEMPLATE_SITE_1 => '商场内部店',
        self::SYS_TEMPLATE_SITE_2 => '商场金街店',
        self::SYS_TEMPLATE_SITE_3 => '社区街边店',
        self::SYS_TEMPLATE_SITE_4 => '社区学校店',
        self::SYS_TEMPLATE_SITE_5 => '校内食堂店',
        self::SYS_TEMPLATE_SITE_6 => '学校商街店',
        self::SYS_TEMPLATE_SITE_7 => '景区内部店',

        self::SYS_TEMPLATE_COMMON  => '常规巡检',
        self::SYS_TEMPLATE_SPECIAL => '专项巡检',

        self::SYS_TEMPLATE_FACTORY_SELF => '工厂自检',
        self::SYS_TEMPLATE_FACTORY_SPECIAL => '专项巡检',

        self::SYS_TEMPLATE_COLLEGE => '新店培训', // 商学院

        self::SYS_TEMPLATE_TOUR => '日常巡检', // 云巡店

        self::SYS_TEMPLATE_MALL_INFO => '商场信息', // 其他
        self::SYS_TEMPLATE_OTHER_MYBANK_REGISTER => '网商进件',

        self::SYS_TEMPLATE_SELF_OPEN  => '开档自检',
        self::SYS_TEMPLATE_SELF_CLOSE => '收档自检',
        self::SYS_TEMPLATE_SELF_WEEK  => '周度自检',
        self::SYS_TEMPLATE_SELF_MONTH => '月度自检',

        self::SYS_TEMPLATE_ACTIVITY_ACTION => '项目行为',

        self::SYS_TEMPLATE_WORKFLOW_RENEW => '续签',
        self::SYS_TEMPLATE_WORKFLOW_WORK_ORDER => '工单',
    ];

    // 专项模板
    const TEMPLATE_SPECIAL_SYS = [
        self::TYPE_3 => self::SYS_TEMPLATE_SPECIAL,
        self::TYPE_5 => self::SYS_TEMPLATE_FACTORY_SPECIAL,
    ];

    // 只允许存在一个的模板
    const TEMPLATE_SINGLE = [
        self::SYS_TEMPLATE_1,
        self::SYS_TEMPLATE_COMMON,
        self::SYS_TEMPLATE_COLLEGE,
    ];

    // 二级系统模板列表
    const TEMPLATE_TYPES = [
        self::TYPE_1 => [
            self::SYS_TEMPLATE_1,
        ],
        self::TYPE_2 => [
            self::SYS_TEMPLATE_SITE_1,
            self::SYS_TEMPLATE_SITE_2,
            self::SYS_TEMPLATE_SITE_3,
            self::SYS_TEMPLATE_SITE_4,
            self::SYS_TEMPLATE_SITE_5,
            self::SYS_TEMPLATE_SITE_6,
            self::SYS_TEMPLATE_SITE_7,
        ],
        self::TYPE_3 => [
            self::SYS_TEMPLATE_COMMON,
            self::SYS_TEMPLATE_SPECIAL,
        ],
        self::TYPE_4 => [
            self::SYS_TEMPLATE_COLLEGE,
        ],
        self::TYPE_5 => [
            self::SYS_TEMPLATE_FACTORY_SELF,
            self::SYS_TEMPLATE_FACTORY_SPECIAL,
        ],
        self::TYPE_7 => [
            self::SYS_TEMPLATE_WORKFLOW_RENEW,
            self::SYS_TEMPLATE_WORKFLOW_WORK_ORDER,
        ],
        self::TYPE_6 => [
            self::SYS_TEMPLATE_TOUR,
        ],
        self::TYPE_8 => [
            self::SYS_TEMPLATE_SELF_OPEN,
            self::SYS_TEMPLATE_SELF_CLOSE,
            self::SYS_TEMPLATE_SELF_WEEK,
            self::SYS_TEMPLATE_SELF_MONTH,
        ],
        self::TYPE_9 => [
            self::SYS_TEMPLATE_ACTIVITY_ACTION,
        ],
        self::TYPE_99 => [
            self::SYS_TEMPLATE_MALL_INFO,
            self::SYS_TEMPLATE_OTHER_MYBANK_REGISTER,
        ],
    ];

    const ANTI_CHEATING_SWITCH_OFF  = 0;
    const ANTI_CHEATING_SWITCH_ON   = 1;
    const ANTI_CHEATING_SWITCH_TEXT = [
        self::ANTI_CHEATING_SWITCH_OFF => "关闭",
        self::ANTI_CHEATING_SWITCH_ON  => "开启",
    ];

    public function getTypeTextAttribute(): string
    {
        return self::TYPE_TEXT[$this->type] ?? '';
    }

    public function getSysTemplateTextAttribute(): string
    {
        return self::SYS_TEMPLATE_TEXT[$this->sysTemplate] ?? '';
    }

    public function positionsRel(): HasMany
    {
        return $this->hasMany(CruiseTemplatePositionModel::class, 'templateId', 'id');
    }

    /**
     * 根据类型，获取id
     * @param int $sysTemplate
     * @return int
     */
    public static function getTemplateIdBySysTemplate(int $sysTemplate): int
    {
        return (int)self::query()
                        ->where('sysTemplate', $sysTemplate)
                        ->where('status', self::STATUS_ON)
                        ->orderByDesc('id')
                        ->value('id');
    }


    private static array $templates;

    public static function templates()
    {
        return self::$templates ?: Cache::remember('cruise_templates', 60, function () {
            return self::query()
                       ->where('status', self::STATUS_ON)
                       ->pluck('name', 'id')
                       ->toArray();
        });
    }

    /**
     * 通过id获取模板名称
     * @param int $id
     * @return string
     */
    public static function getTemplateName(int $id): string
    {
        return self::templates()[$id] ?? '';
    }
}
